package org.danger.t7.common.meters.http;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.micrometer.core.instrument.MeterRegistry;
import org.danger.t7.common.meters.KeyAndTags;
import org.danger.t7.common.meters.Meters;
import org.springframework.web.filter.OncePerRequestFilter;

/**
 * .
 */
@Deprecated
public class HttpMetricFilter extends OncePerRequestFilter {

    private final MeterRegistry meter;

    public HttpMetricFilter(MeterRegistry meter) {
        this.meter = meter;
    }

    /**
     * .
     *
     * @param request
     * @param response    .
     * @param filterChain .
     * @throws ServletException .
     * @throws IOException      .
     */
    @Override
    protected void doFilterInternal(HttpServletRequest request,
                                    HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {


        long startTime = System.currentTimeMillis();

        filterChain.doFilter(request, response);

        Meters.timer(KeyAndTags.key("http_server_requests_seconds")
                .tag("method", request.getMethod().toUpperCase())
                .tag("outcome", "")
                .tag("uri", request.getRequestURI())
                .tag("exception", "None")
                .tag("status", String.valueOf(response.getStatus())))
                .record(System.currentTimeMillis() - startTime, TimeUnit.MILLISECONDS);
    }

}
